package com.fly.demo.main;

import java.util.Date;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.fly.demo.entity.User;
import com.fly.demo.service.UserService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * MainRun
 * 
 * @author 00fly
 * @version [版本号, 2018-11-01]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
public class MainRun
{
    /**
     * main
     * 
     * @param args
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args)
    {
        try (GenericXmlApplicationContext context = new GenericXmlApplicationContext())
        {
            context.setValidating(false);
            context.load("classpath:/spring/spring.xml");
            context.refresh();
            int i = 1;
            for (String beanName : context.getBeanDefinitionNames())
            {
                log.info("{}.\t{}", i, beanName);
                i++;
            }
            UserService userservice = context.getBean(UserService.class);
            if (userservice.selectCount(null) == 0)
            {
                for (i = 0; i < 10; i++)
                {
                    User user = new User();
                    user.setUsername("username_" + RandomStringUtils.randomAlphabetic(5));
                    user.setPassword("password_" + RandomStringUtils.randomAlphabetic(5));
                    user.setName("name_" + RandomStringUtils.randomAlphabetic(5));
                    user.setCreateDate(new Date());
                    user.setAge(RandomUtils.nextInt(10, 30));
                    user.setEnabled(true);
                    userservice.insert(user);
                }
            }
            userservice.testTrans();
        }
    }
}
